package com.kukudb.captcharepeater.serveice;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kukudb.captcharepeater.pojo.Promoters;
import com.kukudb.captcharepeater.pojo.ResponseMessage;
import com.kukudb.captcharepeater.pojo.Users;
import com.kukudb.captcharepeater.repository.PromotersRepository;
import com.kukudb.captcharepeater.repository.UserRepository;
import com.kukudb.captcharepeater.serveice.interFaceTemp.AdminServericeInterface;
import com.kukudb.captcharepeater.utils.JWTUtils;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AdminServericeImpl implements AdminServericeInterface {
    @Autowired
    UserRepository userRepository;

    @Autowired
    PromotersRepository promotersRepository;


    @Override
    public ResponseMessage<String> setIsSuccess(Users users) {
        log.info("setIsSuccess----------{}",users);
        users.setProcessingTime(new Date());
        userRepository.updateById(users);
        return ResponseMessage.success("success");
    }

    @Override
    public ResponseMessage<String> deleteUsers(Users users) {
        int i = userRepository.deleteById(users);

        return ResponseMessage.success("success");
    }

    @Override
    public ResponseMessage<String> addPromoter(Promoters promoters) {
        log.info("添加推广员中：{}",promoters);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("promotion_code", promoters.getPromotionCode());
        Promoters promoters1 = promotersRepository.selectOne(queryWrapper);
        if (promoters1 != null) {
            log.info("推广员已存在");
            throw new RuntimeException("推广员已存在");
        }
        promotersRepository.insert(promoters);
        log.info("添加推广员成功");
        return ResponseMessage.success("添加成功");
    }

    @Override
    public Promoters login(Promoters promoters, HttpSession session) {
        String user =  promoters.getUsername();
        String password = promoters.getPassword();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", user);
        queryWrapper.eq("password", password);

        Promoters promoters1 = promotersRepository.selectOne(queryWrapper);

        if (promoters1 == null) {
            log.info("用户名或密码错误");
            throw new RuntimeException("用户名或密码错误");
        }
        String token = JWTUtils.createToken(promoters1);
        session.setAttribute("token", token);
        return promoters1;
    }

    @Override
    public boolean isAdmin(Integer userId) {
        //判断是否是管理员
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userId);
        Promoters promoters = promotersRepository.selectOne(queryWrapper);
//        log.info("isAdmin----------promoters:{}",promoters);
        if (promoters != null) {
            if (promoters.getIsAdmin() == 1) {
                return true;
            }

        }

//        throw new RuntimeException("用户不存在");
        return false;
    }

    @Override
    public ResponseMessage<List<Users>> getUserList(Integer userId) {
        //先从promotersRepository获取推广码，然后在筛选出他推广的用户输出
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userId);
        Promoters promoters = promotersRepository.selectOne(queryWrapper);
        String promotionCode = promoters.getPromotionCode();
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("promotion_code", promotionCode);
        List<Users> users = userRepository.selectList(queryWrapper1);
        //将推广码转换成用户名
        for (Users user : users) {
            String promotionCode1 = user.getPromotionCode();
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("promotion_code", promotionCode1);
            Promoters promoters1 = promotersRepository.selectOne(queryWrapper2);
            user.setPromotionCode(promoters1.getUsername());
        }
        return new ResponseMessage<>(200, "success", users);

    }

    @Override
    public ResponseMessage<String> deleteUsers(Integer userId, Users users) {
        //判断是不是自己推广的用户，如果是就删除
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userId);
        Promoters promoters = promotersRepository.selectOne(queryWrapper);
        String promotionCode = promoters.getPromotionCode();
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("promotion_code", promotionCode);
        List<Users> users1 = userRepository.selectList(queryWrapper1);
        for (Users user : users1) {
            if (user.getId().equals(users.getId())) {
                userRepository.deleteById(user);
                return ResponseMessage.success("success");
            }
        }

        return null;
    }

    @Override
    public ResponseMessage<String> setIsSuccess(Users users,Integer userId) {
        //判断是不是自己推广的用户，如果是就修改
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userId);
        Promoters promoters = promotersRepository.selectOne(queryWrapper);
        //获取到推广员推广码
        String promotionCode = promoters.getPromotionCode();
        Users users1 = userRepository.selectById(users.getId());
        if (users1.getPromotionCode().equals(promotionCode)) {
            users.setProcessingTime(new Date());
            userRepository.updateById(users);
            return ResponseMessage.success("success");
        }


        return null;
    }

    @Override
    public ResponseMessage<String> updatePromoter(Promoters promoters) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", promoters.getUsername());
        Promoters promoters1 = promotersRepository.selectOne(queryWrapper);
//        promoters1.setUsername(promoters.getUsername());
        promoters1.setPassword(promoters.getPassword());
        promoters1.setPromotionCode(promoters.getPromotionCode());
        promoters1.setEmail(promoters.getEmail());
        promotersRepository.updateById(promoters1);
        return ResponseMessage.success("success");
    }

    @Override
    public ResponseMessage<String> updatePromoter(HttpSession session, Promoters promoters) {
        Integer userId = JWTUtils.getSessionUserId(session);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userId);
        Promoters promoters1 = promotersRepository.selectOne(queryWrapper);
        promoters1.setPassword(promoters.getPassword());
        promoters1.setEmail(promoters.getEmail());
        promotersRepository.updateById(promoters1);
        return ResponseMessage.success("success");

    }

    @Override
    public ResponseMessage<String> deletePromoter(Promoters promoters) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", promoters.getUsername());
        Promoters promoters1 = promotersRepository.selectOne(queryWrapper);
        log.info("deletePromoter----------promoters1:{}",promoters1);
        if (promoters1 == null) {
            throw new RuntimeException("推广员不存在");

        }
        promotersRepository.deleteById(promoters1);
        return ResponseMessage.success("success");

    }

    @Override
    public ResponseMessage<String> deletePromoter(HttpSession session, Promoters promoters) {
        Integer userId = JWTUtils.getSessionUserId(session);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userId);
        Promoters promoters1 = promotersRepository.selectOne(queryWrapper);
        promotersRepository.deleteById(promoters1);
        return ResponseMessage.success("success");

    }

    @Override
    public ResponseMessage<List<Promoters>> getPromoterList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        List<Promoters> promoters = promotersRepository.selectList(queryWrapper);
        return new ResponseMessage<>(200, "success", promoters);
    }

    @Override
    public ResponseMessage<List<Users>> getUserList() {
        log.info("getUserList----------");
        QueryWrapper<Users>  queryWrapper = new QueryWrapper<>();
        queryWrapper.orderBy(true,false,"created_at");
        List<Users> users = userRepository.selectList(queryWrapper);
        //将推广码转换成用户名
        for (Users user : users) {
            String promotionCode = user.getPromotionCode();
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("promotion_code", promotionCode);
            Promoters promoters = promotersRepository.selectOne(queryWrapper1);
            user.setPromotionCode(promoters.getUsername());
        }
        return new ResponseMessage<>(200, "success", users);
    }
}
